package com.hymjweb.demo.hymj.customer_manage.contact.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.pojoContrast.FieldMeta;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.goods_site.detail.bean.XtAirportGoodsSiteDetailPojo;
import com.hymjweb.demo.hymj.customer_manage.contact.bean.BCustomerContactPojo;
import com.hymjweb.demo.hymj.customer_manage.mapper.BCustomerMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BCustomerContactMapper {



    class BCustomerContactMapperProvider{

        public String queryByUk(String customerId, String name, String phone){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_contact t ");
                WHERE("t.customer_id = " + DataTypeUtil.quoteStr(customerId) + " and t.name = " + DataTypeUtil.quoteStr(name) + " and t.phone=" + DataTypeUtil.quoteStr(phone));
            }}.toString();

            return null;
        }

        public String findByBCustomerId(String bCustomerId) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_contact t ");
                WHERE("t.customer_id = " + DataTypeUtil.quoteStr(bCustomerId));
            }}.toString();

            return  sql;
        }

        public String insert(BCustomerContactPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_customer_contact");
                VALUES("id","#{id}");
                VALUES("customer_id","#{customerId}");
                VALUES("name","#{name}");
                VALUES("full_pinyin","#{fullPinyin}");
                VALUES("camel_char_pinyin","#{camelCharPinyin}");
                VALUES("phone","#{phone}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(BCustomerContactPojo pojo){
            String sql= new SQL() {{
                UPDATE("b_customer_contact t");
                SET("t.customer_id = #{customerId}");
                SET("t.name = #{name}");
                SET("t.full_pinyin = #{fullPinyin}");
                SET("t.camel_char_pinyin = #{camelCharPinyin}");
                SET("t.phone = #{phone}");
                SET("t.comment = #{comment}");
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("b_customer_contact");
                WHERE("id = " + DataTypeUtil.quoteStr(id) );
            }}.toString();
            return sql;
        }

        public String findUK(String phone,String bCustomerId,String exceptId) {

            String sql= new SQL() {{
                SELECT("t.id,t.name,t.phone");
                FROM("b_customer_contact t");
                WHERE(" t.phone = " + DataTypeUtil.quoteStr(phone));
                WHERE(" t.customer_id = " + DataTypeUtil.quoteStr(bCustomerId));
            }}.toString();

            if(!DataTypeUtil.isEmptyStr(exceptId)){
                sql += " and t.id != " + DataTypeUtil.quoteStr(exceptId);
            }

            return  sql;
        }

    }

    @Results(id = "bCustomerContactPojoMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "customer_id", property = "customerId"),
            @Result(column = "name", property = "name"),
            @Result(column = "full_pinyin", property = "fullPinyin"),
            @Result(column = "camel_char_pinyin", property = "camelCharPinyin"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = BCustomerContactMapperProvider.class, method = "findByBCustomerId")
    List<BCustomerContactPojo> findByBCustomerId(String bCustomerId);

    @ResultMap("bCustomerContactPojoMapId")
    @SelectProvider(type = BCustomerContactMapperProvider.class, method = "findUK")
    BCustomerContactPojo findUK(String phone, String bCustomerId, String exceptId);


    @ResultMap("bCustomerContactPojoMapId")
    @SelectProvider(type = BCustomerContactMapperProvider.class, method = "queryByUk")
    BCustomerContactPojo queryByUk(String customerId, String name, String phone);


    @InsertProvider(type = BCustomerContactMapperProvider.class, method = "insert")
    int insert(BCustomerContactPojo pojo);

    @UpdateProvider(type = BCustomerContactMapperProvider.class, method = "update")
    int update(BCustomerContactPojo pojo);

    @DeleteProvider(type = BCustomerContactMapperProvider.class, method = "delete")
    int delete(String id);

}
